package com.lottery.pay.Model.weixin;

import com.lottery.pay.Constants.WeixinConstant;
import org.apache.commons.lang.RandomStringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 微信统一下单所需参数，使用指定命名格式
 */
//@Configuration
//@ImportResource("classpath:spring-mvc.xml")
public class OrderParam {

	private String appid = "";// 公众账号ID
	private String mch_id = "";// 商户号
	private String device_info = "";// 终端设备号(门店号或收银设备ID)，注意：PC网页或公众号内支付请传"WEB"
	private String nonce_str = RandomStringUtils.randomAlphanumeric(20);// 生成随机字符串
	private String sign = "";// 签名
	private String body = "";// 商品描述
	private String detail = "";// 商品详情
	private String attach = "";// 附加数据
	private String out_trade_no = "";// 商户订单号
	private String fee_type = "CNY";// 货币类型 默认人民币：CNY
	private String total_fee = "";// 总金额 订单总金额，单位为分
	private String spbill_create_ip = "";// APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP。
	private String time_start = setTime_start();// 交易起始时间 格式为yyyyMMddHHmmss
	private String time_expire = setTime_expire(10 * 60);// 交易结束时间
															// 注意：最短失效时间间隔必须大于5分钟
															// 这里取间隔10分钟
	private String goods_tag = "";// 商品标记
	private String notify_url = WeixinConstant.WEIXIN_PAYMENT_RESULT;// 通知地址 接收微信支付异步通知回调地址
	private String trade_type = "";// JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付，
	private String product_id = "";// 商品ID
	private String limit_pay = "no_credit";// 指定支付方式 no_credit--指定不能使用信用卡支付
	private String openid = "";// 用户标识

	public OrderParam(String appid, String mch_id, String body, String detail, String attach,
                      String out_trade_no, String total_fee, String spbill_create_ip,
                      String goods_tag, String trade_type, String product_id, String openid, String timeStart) {
		this.appid = appid;
		this.mch_id = mch_id;
		this.body = body;
		this.detail = detail;
		this.attach = attach;
		this.out_trade_no = out_trade_no;
		this.total_fee = total_fee;
		this.spbill_create_ip = spbill_create_ip;
		this.goods_tag = goods_tag;
		this.trade_type = trade_type;
		this.product_id = product_id;
		this.openid = openid;
		this.time_start = timeStart;

	}

	public String getAppid() {
		return appid;
	}

	public String getMch_id() {
		return mch_id;
	}

	public String getDevice_info() {
		return device_info;
	}

	public String getNonce_str() {
		return nonce_str;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getAttach() {
		return attach;
	}

	public void setAttach(String attach) {
		this.attach = attach;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	public String getFee_type() {
		return fee_type;
	}

	public String getTotal_fee() {
		return total_fee;
	}

	public void setTotal_fee(String total_fee) {
		this.total_fee = total_fee;
	}

	public String getSpbill_create_ip() {
		return spbill_create_ip;
	}

	public void setSpbill_create_ip(String spbill_create_ip) {
		this.spbill_create_ip = spbill_create_ip;
	}

	public String getTime_start() {
		return time_start;
	}

	/**
	 * 生成开始时间
	 */
	public String setTime_start() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	public String getTime_expire() {
		return time_expire;
	}

	/**
	 * 生成结束时间
	 *
	 */
	public String setTime_expire(int time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			Date date = format.parse(this.time_start);
			Long nowTime = date.getTime();
			Long endTime = nowTime + time * 1000;
			Date endDate = new Date(endTime);
			return format.format(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
			Long nowTime = System.currentTimeMillis();
			Long endTime = nowTime + time * 1000;
			Date endDate = new Date(endTime);
			return format.format(endDate);
		}
	}

	public String getGoods_tag() {
		return goods_tag;
	}

	public void setGoods_tag(String goods_tag) {
		this.goods_tag = goods_tag;
	}

	public String getNotify_url() {
		return notify_url;
	}

	public void setNotify_url(String notify_url) {
		this.notify_url = notify_url;
	}

	public String getTrade_type() {
		return trade_type;
	}

	public String getProduct_id() {
		return product_id;
	}

	public void setProduct_id(String product_id) {
		this.product_id = product_id;
	}

	public String getLimit_pay() {
		return limit_pay;
	}

	public void setLimit_pay(String limit_pay) {
		this.limit_pay = limit_pay;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	/**
	 * 转化成指定xml格式
	 */
	public String toXmlString(OrderParam orderParam) {
		String xmlString = String.format(WeixinConstant.PAYOFF_MSG,
				orderParam.appid, orderParam.mch_id, orderParam.device_info,
				orderParam.nonce_str, orderParam.sign, orderParam.body,
				orderParam.detail, orderParam.attach, orderParam.out_trade_no,
				orderParam.fee_type, orderParam.total_fee,
				orderParam.spbill_create_ip, orderParam.time_start,
				orderParam.time_expire, orderParam.goods_tag,
				orderParam.notify_url, orderParam.trade_type,
				orderParam.product_id, orderParam.limit_pay
//				, orderParam.openid
		);
		return xmlString;
	}

	/**
	 * 转化成指定xml格式
	 */
	public String toJsXmlString(OrderParam orderParam) {
		String xmlString = String.format(WeixinConstant.JS_PAYOFF_MSG,
				orderParam.appid, orderParam.mch_id, orderParam.device_info,
				orderParam.nonce_str, orderParam.sign, orderParam.body,
				orderParam.detail, orderParam.attach, orderParam.out_trade_no,
				orderParam.fee_type, orderParam.total_fee,
				orderParam.spbill_create_ip, orderParam.time_start,
				orderParam.time_expire, orderParam.goods_tag,
				orderParam.notify_url, orderParam.trade_type,
				orderParam.product_id, orderParam.limit_pay, orderParam.openid);
		return xmlString;
	}

}
